import redis
import pymysql

from flask import Flask, request, render_template, make_response, jsonify, session

app = Flask(__name__)

# ################################################
# 配置 mysql
# ################################################
conn = pymysql.connect(host='user-db', user="root", passwd="123456", database="userdb")
cursor = conn.cursor()

##################################################

# ################################################
# 配置 redis
# ################################################
conn_redis = redis.Redis(host='fanggx.xyz', port=3390)

good_list = ['小熊', '玫瑰', '杯子', '眼镜']

for i, good in enumerate(good_list):
    sql_selectRemain = "SELECT remainCount FROM `goodTable` WHERE name = \'%s\'" % good
    cursor.execute(sql_selectRemain)
    remainNum = cursor.fetchone()
    conn_redis.set(i, int(remainNum[0]))

##################################################


@app.route("/", methods=['GET', 'POST'])
@app.route('/mainPage.html', methods=['GET', 'POST'])
def dormList():
    if request.method == "GET":
        return render_template("/mainPage.html")

@app.route('/getInfo', methods=['GET', 'POST'])
def getInfo():
    if request.method == "POST":
        infoGetted = request.json
        user_code = infoGetted["code"]
        
        remains = [1, 2]
        return make_response(jsonify({ 'remains': remains }), 200)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
    if request.method == "POST":
        gift_choosed = request.json
        
        return make_response(jsonify({ 'response': "Getted!" }), 200)

app.run(host='0.0.0.0', port=3353)